/**
 * 随机计算
 *
 * @param {number} n
 * @return {number}
 */
function randπ(n) {
  let count = 0;
  let x, y;
  for (let i = 0; i < n; i++) {
    x = Math.random();
    y = Math.random();
    if (x * x + y * y < 1) {
      count++;
    }
  }
  return (4 * count) / n;
}

/**
 * 级数计算
 *
 * @param {number} n
 * @return {number}
 */
function seriesπ(n) {
  let sum = 0;
  for (let i = 1; i <= n; i++) {
    sum += 1 / (i * i);
  }
  return Math.sqrt(sum * 6);
}

/**
 * 网格计算
 *
 * @param {number} n
 * @return {number}
 */
function gridπ(n) {
  let sum = 0;
  for (let i = 0; i < n; i++) {
    sum += Math.sqrt(n * n - i * i);
  }
  return (4 * sum) / n / n;
}

const π = 3.141592653589793;
const rand = randπ(100000000);
console.log(rand);
console.log(Math.abs(π - rand).toFixed(12));
const series = seriesπ(1000000000);
console.log(series);
console.log(Math.abs(π - series).toFixed(12));
const grid = gridπ(1000000000);
console.log(grid);
console.log(Math.abs(π - grid).toFixed(12));
